package javolution.text;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.ogg.DefaultOggSeeker;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import javax.realtime.MemoryArea;
import javolution.JavolutionError;
import javolution.context.ObjectFactory;
import javolution.io.UTF8StreamWriter;
import javolution.lang.MathLib;
import javolution.lang.Realtime;
import javolution.lang.Reusable;

/* loaded from: classes2.dex */
public class TextBuilder implements Appendable, CharSequence, Reusable, Realtime, Serializable {
    private static final int C0 = 32;
    private static final int C1 = 4096;
    private static final int C2 = 2097152;
    private static final int D0 = 5;
    private static final int D1 = 7;
    private static final int D2 = 9;
    private static final int D3 = 11;
    private static final int M0 = 31;
    private static final int M1 = 127;
    private static final int M2 = 511;
    private static final int R1 = 5;
    private static final int R2 = 12;
    private static final int R3 = 21;
    private int _capacity;
    private final char[] _chars0;
    private char[][] _chars1;
    private char[][][] _chars2;
    private char[][][][] _chars3;
    private int _length;
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.text.TextBuilder.1
        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new TextBuilder();
        }
    };
    private static final long[] POW10_LONG = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, C.NANOS_PER_SECOND, 10000000000L, 100000000000L, 1000000000000L, 10000000000000L, 100000000000000L, 1000000000000000L, 10000000000000000L, 100000000000000000L, 1000000000000000000L};
    private static final UTF8StreamWriter SYSTEM_OUT_WRITER = new UTF8StreamWriter().setOutput(System.out);
    private static final char[] DIGIT_TO_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    public TextBuilder() {
        this._chars0 = new char[32];
        this._capacity = 32;
    }

    public TextBuilder(int i) {
        this._chars0 = new char[32];
        this._capacity = 32;
        while (i > this._capacity) {
            increaseCapacity();
        }
    }

    public TextBuilder(CharSequence charSequence) {
        this._chars0 = new char[32];
        this._capacity = 32;
        append(charSequence);
    }

    public TextBuilder(String str) {
        this._chars0 = new char[32];
        this._capacity = 32;
        append(str);
    }

    static /* synthetic */ int access$012(TextBuilder textBuilder, int i) {
        int i2 = textBuilder._capacity + i;
        textBuilder._capacity = i2;
        return i2;
    }

    private TextBuilder append2(char c) {
        if (this._length >= this._capacity) {
            increaseCapacity();
        }
        int i = this._length;
        this._length = i + 1;
        if (i < 4096) {
            this._chars1[i >> 5][i & 31] = c;
        } else if (i < 2097152) {
            this._chars2[i >> 12][(i >> 5) & M1][i & 31] = c;
        } else {
            this._chars3[i >> 21][(i >> 12) & 511][(i >> 5) & M1][i & 31] = c;
        }
        return this;
    }

    private void append2(int i, int i2) {
        char c;
        if (i <= (-i2)) {
            append2(i / i2, i2);
            c = DIGIT_TO_CHAR[-(i % i2)];
        } else {
            c = DIGIT_TO_CHAR[-i];
        }
        append(c);
    }

    private void append2(long j, int i) {
        char c;
        if (j <= (-i)) {
            long j2 = i;
            append2(j / j2, i);
            c = DIGIT_TO_CHAR[(int) (-(j % j2))];
        } else {
            c = DIGIT_TO_CHAR[(int) (-j)];
        }
        append(c);
    }

    private final TextBuilder appendNoCheck(String str, int i, int i2) {
        int i3 = (this._length + i2) - i;
        while (this._capacity < i3) {
            increaseCapacity();
        }
        int i4 = this._length;
        while (i < i2) {
            char[] charsAt = charsAt(i4);
            int i5 = i4 & 31;
            int min = MathLib.min(32 - i5, i2 - i);
            int i6 = i + min;
            str.getChars(i, i6, charsAt, i5);
            i4 += min;
            i = i6;
        }
        this._length = i3;
        return this;
    }

    private final TextBuilder appendNoCheck(Text text, int i, int i2) {
        int i3 = (this._length + i2) - i;
        while (this._capacity < i3) {
            increaseCapacity();
        }
        int i4 = this._length;
        while (i < i2) {
            char[] charsAt = charsAt(i4);
            int i5 = i4 & 31;
            int min = MathLib.min(32 - i5, i2 - i);
            int i6 = i + min;
            text.getChars(i, i6, charsAt, i5);
            i4 += min;
            i = i6;
        }
        this._length = i3;
        return this;
    }

    private void increaseCapacity() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.text.TextBuilder.2
            @Override // java.lang.Runnable
            public void run() {
                int i = TextBuilder.this._capacity;
                TextBuilder.access$012(TextBuilder.this, 32);
                if (i < 4096) {
                    if (TextBuilder.this._chars1 == null) {
                        TextBuilder.this._chars1 = new char[128];
                    }
                    TextBuilder.this._chars1[i >> 5] = new char[32];
                } else if (i < 2097152) {
                    if (TextBuilder.this._chars2 == null) {
                        TextBuilder.this._chars2 = new char[512][];
                    }
                    int i2 = i >> 12;
                    if (TextBuilder.this._chars2[i2] == null) {
                        TextBuilder.this._chars2[i2] = new char[128];
                    }
                    TextBuilder.this._chars2[i2][(i >> 5) & TextBuilder.M1] = new char[32];
                } else {
                    if (TextBuilder.this._chars3 == null) {
                        TextBuilder.this._chars3 = new char[2048][][];
                    }
                    int i3 = i >> 21;
                    if (TextBuilder.this._chars3[i3] == null) {
                        TextBuilder.this._chars3[i3] = new char[512][];
                    }
                    int i4 = (i >> 12) & 511;
                    if (TextBuilder.this._chars3[i3][i4] == null) {
                        TextBuilder.this._chars3[i3][i4] = new char[128];
                    }
                    TextBuilder.this._chars3[i3][i4][(i >> 5) & TextBuilder.M1] = new char[32];
                }
            }
        });
    }

    public static TextBuilder newInstance() {
        TextBuilder textBuilder = (TextBuilder) FACTORY.object();
        textBuilder._length = 0;
        return textBuilder;
    }

    public static void recycle(TextBuilder textBuilder) {
        FACTORY.recycle(textBuilder);
    }

    private void writeDigits(int i, boolean z) {
        boolean z2 = true;
        if (i >= 10000) {
            int i2 = 50000;
            if (i >= 50000) {
                int i3 = 80000;
                if (i < 80000) {
                    i3 = 70000;
                    if (i >= 70000) {
                        append('7');
                        i -= i3;
                    } else {
                        if (i >= 60000) {
                            append('6');
                            i2 = 60000;
                        } else {
                            append('5');
                        }
                        i -= i2;
                    }
                } else if (i >= 90000) {
                    append('9');
                    i2 = 90000;
                    i -= i2;
                } else {
                    append('8');
                    i -= i3;
                }
                z = true;
            } else {
                if (i >= 30000) {
                    i2 = 40000;
                    if (i >= 40000) {
                        append('4');
                        i -= i2;
                    } else {
                        append('3');
                        i -= 30000;
                    }
                } else if (i >= 20000) {
                    append('2');
                    i -= 20000;
                } else {
                    append('1');
                    i -= 10000;
                }
                z = true;
            }
        } else if (z) {
            append('0');
        }
        if (i >= 1000) {
            if (i >= 5000) {
                if (i >= 8000) {
                    if (i >= 9000) {
                        append('9');
                        i -= 9000;
                    } else {
                        append('8');
                        i -= 8000;
                    }
                } else if (i >= 7000) {
                    append('7');
                    i -= 7000;
                } else if (i >= 6000) {
                    append('6');
                    i -= 6000;
                } else {
                    append('5');
                    i -= 5000;
                }
            } else if (i >= 3000) {
                if (i >= 4000) {
                    append('4');
                    i -= 4000;
                } else {
                    append('3');
                    i -= 3000;
                }
            } else if (i >= 2000) {
                append('2');
                i -= 2000;
            } else {
                append('1');
                i -= 1000;
            }
            z = true;
        } else if (z) {
            append('0');
        }
        if (i < 100) {
            if (z) {
                append('0');
            }
            z2 = z;
        } else if (i >= 500) {
            if (i >= 800) {
                if (i >= 900) {
                    append('9');
                    i -= 900;
                } else {
                    append('8');
                    i -= 800;
                }
            } else if (i >= 700) {
                append('7');
                i -= 700;
            } else if (i >= 600) {
                append('6');
                i -= 600;
            } else {
                append('5');
                i -= 500;
            }
        } else if (i >= 300) {
            if (i >= 400) {
                append('4');
                i -= 400;
            } else {
                append('3');
                i -= 300;
            }
        } else if (i >= 200) {
            append('2');
            i -= 200;
        } else {
            append('1');
            i -= 100;
        }
        if (i >= 10) {
            if (i >= 50) {
                if (i >= 80) {
                    if (i >= 90) {
                        append('9');
                        i -= 90;
                    } else {
                        append('8');
                        i -= 80;
                    }
                } else if (i >= 70) {
                    append('7');
                    i -= 70;
                } else if (i >= 60) {
                    append('6');
                    i -= 60;
                } else {
                    append('5');
                    i -= 50;
                }
            } else if (i >= 30) {
                if (i >= 40) {
                    append('4');
                    i -= 40;
                } else {
                    append('3');
                    i -= 30;
                }
            } else if (i >= 20) {
                append('2');
                i -= 20;
            } else {
                append('1');
                i -= 10;
            }
        } else if (z2) {
            append('0');
        }
        append(DIGIT_TO_CHAR[i]);
    }

    @Override // java.lang.Appendable
    public final TextBuilder append(char c) {
        int i = this._length;
        if (i >= 32) {
            return append2(c);
        }
        char[] cArr = this._chars0;
        this._length = i + 1;
        cArr[i] = c;
        return this;
    }

    public final TextBuilder append(double d) {
        return append(d, -1, MathLib.abs(d) >= 1.0E7d || MathLib.abs(d) < 0.001d, false);
    }

    public final TextBuilder append(double d, int i, boolean z, boolean z2) {
        long longPow10;
        String str;
        if (i > 19) {
            throw new IllegalArgumentException("digits: " + i);
        }
        if (d != d) {
            str = "NaN";
        } else {
            if (d < 0.0d) {
                d = -d;
                append('-');
            }
            if (d == Double.POSITIVE_INFINITY) {
                str = "Infinity";
            } else if (d != 0.0d) {
                int floorLog10 = MathLib.floorLog10(d);
                if (i < 0) {
                    i = 16;
                    longPow10 = MathLib.toLongPow10(d, 16 - floorLog10);
                    long j = longPow10 / 10;
                    if (MathLib.toDoublePow10(j, (floorLog10 - 16) + 1) == d) {
                        longPow10 = j;
                    } else {
                        i = 17;
                    }
                } else {
                    longPow10 = MathLib.toLongPow10(d, (i - 1) - floorLog10);
                }
                if (z || floorLog10 >= i) {
                    int i2 = i - 1;
                    long j2 = POW10_LONG[i2];
                    int i3 = (int) (longPow10 / j2);
                    append(DIGIT_TO_CHAR[i3]);
                    appendFraction(longPow10 - (j2 * i3), i2, z2);
                    append('E');
                    append(floorLog10);
                } else {
                    if (floorLog10 < 0) {
                        append('0');
                    } else {
                        long j3 = POW10_LONG[(i - floorLog10) - 1];
                        long j4 = longPow10 / j3;
                        append(j4);
                        longPow10 -= j3 * j4;
                    }
                    appendFraction(longPow10, (i - floorLog10) - 1, z2);
                }
                return this;
            } else if (i != 1) {
                if (z2) {
                    append("0.0");
                    for (int i4 = 2; i4 < i; i4++) {
                        append('0');
                    }
                    return this;
                }
                return append("0.0");
            } else {
                str = "0.";
            }
        }
        return append(str);
    }

    public final TextBuilder append(float f) {
        return append(f, 10, ((double) MathLib.abs(f)) >= 1.0E7d || ((double) MathLib.abs(f)) < 0.001d, false);
    }

    public final TextBuilder append(int i) {
        if (i < 0) {
            if (i == Integer.MIN_VALUE) {
                return append("-2147483648");
            }
            i = -i;
            append('-');
        }
        boolean z = false;
        if (i >= 100000) {
            int i2 = i / DefaultOggSeeker.MATCH_BYTE_RANGE;
            writeDigits(i2, false);
            i -= i2 * DefaultOggSeeker.MATCH_BYTE_RANGE;
            z = true;
        }
        writeDigits(i, z);
        return this;
    }

    public final TextBuilder append(int i, int i2) {
        if (i2 == 10) {
            return append(i);
        }
        if (i2 < 2 || i2 > 36) {
            throw new IllegalArgumentException("radix: " + i2);
        }
        if (i < 0) {
            append('-');
        } else {
            i = -i;
        }
        append2(i, i2);
        return this;
    }

    public final TextBuilder append(long j) {
        if (j < 0) {
            if (j == Long.MIN_VALUE) {
                return append("-9223372036854775808");
            }
            j = -j;
            append('-');
        }
        boolean z = false;
        if (j >= 1000000000000000L) {
            int i = (int) (j / 1000000000000000L);
            writeDigits(i, false);
            j -= i * 1000000000000000L;
            z = true;
        }
        if (z || j >= 10000000000L) {
            int i2 = (int) (j / 10000000000L);
            writeDigits(i2, z);
            j -= i2 * 10000000000L;
            z = true;
        }
        if (z || j >= 100000) {
            int i3 = (int) (j / 100000);
            writeDigits(i3, z);
            j -= i3 * 100000;
            z = true;
        }
        writeDigits((int) j, z);
        return this;
    }

    public final TextBuilder append(long j, int i) {
        if (i == 10) {
            return append(j);
        }
        if (i < 2 || i > 36) {
            throw new IllegalArgumentException("radix: " + i);
        }
        if (j < 0) {
            append('-');
        } else {
            j = -j;
        }
        append2(j, i);
        return this;
    }

    @Override // java.lang.Appendable
    public final TextBuilder append(CharSequence charSequence) {
        return charSequence == null ? append("null") : append(charSequence, 0, charSequence.length());
    }

    @Override // java.lang.Appendable
    public final TextBuilder append(CharSequence charSequence, int i, int i2) {
        if (charSequence == null) {
            return append("null");
        }
        if (i < 0 || i2 < 0 || i > i2 || i2 > charSequence.length()) {
            throw new IndexOutOfBoundsException();
        }
        while (i < i2) {
            append(charSequence.charAt(i));
            i++;
        }
        return this;
    }

    public final TextBuilder append(Object obj) {
        return obj instanceof String ? append((String) obj) : append(Text.valueOf(obj));
    }

    public final TextBuilder append(String str) {
        return str == null ? append("null") : appendNoCheck(str, 0, str.length());
    }

    public final TextBuilder append(String str, int i, int i2) {
        if (str == null) {
            return append("null");
        }
        if (i < 0 || i2 < 0 || i > i2 || i2 > str.length()) {
            throw new IndexOutOfBoundsException();
        }
        return appendNoCheck(str, i, i2);
    }

    public final TextBuilder append(Text text) {
        return text == null ? append("null") : appendNoCheck(text, 0, text.length());
    }

    public final TextBuilder append(Text text, int i, int i2) {
        if (text == null) {
            return append("null");
        }
        if (i < 0 || i2 < 0 || i > i2 || i2 > text.length()) {
            throw new IndexOutOfBoundsException();
        }
        return appendNoCheck(text, i, i2);
    }

    public final TextBuilder append(boolean z) {
        return append(z ? "true" : "false");
    }

    public final TextBuilder append(char[] cArr) {
        return append(cArr, 0, cArr.length);
    }

    public final TextBuilder append(char[] cArr, int i, int i2) {
        int i3;
        if (i < 0 || i2 < 0 || (i3 = i2 + i) > cArr.length) {
            throw new IndexOutOfBoundsException();
        }
        while (i < i3) {
            append(cArr[i]);
            i++;
        }
        return this;
    }

    final void appendFraction(long j, int i, boolean z) {
        append('.');
        int digitLength = MathLib.digitLength(j);
        if (digitLength == 0 && !z) {
            append('0');
            return;
        }
        while (digitLength < i) {
            append('0');
            digitLength++;
        }
        if (j != 0) {
            append(j);
        }
        if (z) {
            return;
        }
        int i2 = 0;
        while (charAt((this._length - i2) - 1) == '0') {
            i2++;
        }
        setLength(this._length - i2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void appendTo(StringBuffer stringBuffer) {
        stringBuffer.append(this._chars0, 0, this._length);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void appendTo(StringBuilder sb) {
        sb.append(this._chars0, 0, this._length);
    }

    @Override // java.lang.CharSequence
    public final char charAt(int i) {
        if (i < 0 || i >= this._length) {
            throw new IndexOutOfBoundsException();
        }
        return charsAt(i)[i & 31];
    }

    final char[] charsAt(int i) {
        return i < 32 ? this._chars0 : i < 4096 ? this._chars1[i >> 5] : i < 2097152 ? this._chars2[i >> 12][(i >> 5) & M1] : this._chars3[i >> 21][(i >> 12) & 511][(i >> 5) & M1];
    }

    public final TextBuilder clear() {
        this._length = 0;
        return this;
    }

    public final boolean contentEquals(CharSequence charSequence) {
        if (charSequence.length() != this._length) {
            return false;
        }
        char[] charsAt = charsAt(0);
        int i = 0;
        while (i < this._length) {
            int i2 = i + 1;
            if (charsAt[i & 31] != charSequence.charAt(i)) {
                return false;
            }
            if ((i2 & 31) == 0) {
                charsAt = charsAt(i2);
            }
            i = i2;
        }
        return true;
    }

    public final boolean contentEquals(String str) {
        if (str.length() != this._length) {
            return false;
        }
        char[] charsAt = charsAt(0);
        int i = 0;
        while (i < this._length) {
            int i2 = i + 1;
            if (charsAt[i & 31] != str.charAt(i)) {
                return false;
            }
            if ((i2 & 31) == 0) {
                charsAt = charsAt(i2);
            }
            i = i2;
        }
        return true;
    }

    public final TextBuilder delete(int i, int i2) {
        if (i < 0 || i2 < 0 || i > i2 || i2 > length()) {
            throw new IndexOutOfBoundsException();
        }
        int i3 = i;
        int i4 = i2;
        while (true) {
            int i5 = this._length;
            if (i4 >= i5) {
                this._length = i5 - (i2 - i);
                return this;
            }
            setCharAt(i3, charAt(i4));
            i3++;
            i4++;
        }
    }

    public final boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof TextBuilder) {
            TextBuilder textBuilder = (TextBuilder) obj;
            if (this._length != textBuilder._length) {
                return false;
            }
            int i = 0;
            while (i < this._length) {
                int i2 = i + 1;
                if (charAt(i) != textBuilder.charAt(i)) {
                    return false;
                }
                i = i2;
            }
            return true;
        }
        return false;
    }

    public final void getChars(int i, int i2, char[] cArr, int i3) {
        if (i < 0 || i > i2 || i2 > this._length) {
            throw new IndexOutOfBoundsException();
        }
        while (i < i2) {
            char[] charsAt = charsAt(i);
            int i4 = i & 31;
            int min = MathLib.min(32 - i4, i2 - i);
            System.arraycopy(charsAt, i4, cArr, i3, min);
            i += min;
            i3 += min;
        }
    }

    public final int hashCode() {
        int i = 0;
        for (int i2 = 0; i2 < this._length; i2++) {
            i = (i * 31) + charAt(i2);
        }
        return i;
    }

    public final TextBuilder insert(int i, CharSequence charSequence) {
        int i2;
        if (i < 0 || i > this._length) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        int length = charSequence.length();
        this._length += length;
        while (true) {
            i2 = this._length;
            if (i2 < this._capacity) {
                break;
            }
            increaseCapacity();
        }
        int i3 = i2 - length;
        while (true) {
            i3--;
            if (i3 < i) {
                break;
            }
            setCharAt(i3 + length, charAt(i3));
        }
        int length2 = charSequence.length();
        while (true) {
            length2--;
            if (length2 < 0) {
                return this;
            }
            setCharAt(i + length2, charSequence.charAt(length2));
        }
    }

    @Override // java.lang.CharSequence
    public final int length() {
        return this._length;
    }

    public void print() {
        try {
            synchronized (SYSTEM_OUT_WRITER) {
                print(SYSTEM_OUT_WRITER);
                SYSTEM_OUT_WRITER.flush();
            }
        } catch (IOException e) {
            throw new JavolutionError(e);
        }
    }

    public void print(Writer writer) throws IOException {
        for (int i = 0; i < this._length; i += 32) {
            writer.write(charsAt(i), 0, MathLib.min(32, this._length - i));
        }
    }

    public void println() {
        try {
            synchronized (SYSTEM_OUT_WRITER) {
                println(SYSTEM_OUT_WRITER);
                SYSTEM_OUT_WRITER.flush();
            }
        } catch (IOException e) {
            throw new JavolutionError(e);
        }
    }

    public void println(Writer writer) throws IOException {
        print(writer);
        writer.write(10);
    }

    @Override // javolution.lang.Reusable
    public final void reset() {
        this._length = 0;
    }

    public final TextBuilder reverse() {
        int i = this._length - 1;
        int i2 = (i - 1) >> 1;
        while (i2 >= 0) {
            char charAt = charAt(i2);
            int i3 = i - i2;
            setCharAt(i2, charAt(i3));
            i2--;
            setCharAt(i3, charAt);
        }
        return this;
    }

    public final void setCharAt(int i, char c) {
        if (i >= 0 && i < this._length) {
            charsAt(i)[i & 31] = c;
            return;
        }
        throw new IndexOutOfBoundsException("index: " + i);
    }

    public final void setLength(int i) {
        if (i < 0) {
            throw new IndexOutOfBoundsException();
        }
        int i2 = this._length;
        if (i <= i2) {
            this._length = i;
            return;
        }
        while (true) {
            int i3 = i2 + 1;
            if (i2 >= i) {
                return;
            }
            append((char) 0);
            i2 = i3;
        }
    }

    @Override // java.lang.CharSequence
    public final CharSequence subSequence(int i, int i2) {
        if (i < 0 || i2 < 0 || i > i2 || i2 > this._length) {
            throw new IndexOutOfBoundsException();
        }
        return Text.valueOf(this, i, i2);
    }

    @Override // java.lang.CharSequence
    public final String toString() {
        int i = this._length;
        char[] cArr = new char[i];
        getChars(0, i, cArr, 0);
        return new String(cArr, 0, this._length);
    }

    @Override // javolution.lang.Realtime
    public final Text toText() {
        return Text.valueOf(this, 0, this._length);
    }
}
